const {
  lists,
  menus,
  banners,
  tabs
} = require('./mock')
const bodyParser = require('body-parser');
// 注意： 倒入必须加入 .default
const AlipaySdk = require('alipay-sdk').default;
// TypeScript，可以使用 import AlipaySdk from 'alipay-sdk';
// 普通公钥模式

const alipaySdk = new AlipaySdk({
  // appid
  appId: '2021000119601763',
  // 应用私钥
  privateKey: 'MIIEowIBAAKCAQEAw8n+AicnWKCsuW0IaBaWBBU07cMuBLvDPG9AXjSOwIcEH9udgbwvLhYL2FNVL5icv9Dd/VxDBfh6ruNqovhatq1lX8psa32+MiuJbfVqysoxnuGboV9aXfl8XDcrrzW42JVkTzfy73PC6iOeccYH9YFzL8jJdQN4Iov9swAGVA7n87TkI/I+NhI/hz3bC7AXYjF9Ozi+/pXRel/YsptfmmHteSuQ9R1N7q1pQceuF5fSPh3eVEKn94OvO1fzmQdR0uBu3c0RFhWD1WaINsKSsSP0vihDUwNJK74XoTrbZVnvOhdpKTCH8sNqM8ST+M3UZ+WO/Ia1Q1imS1dMG6jM+QIDAQABAoIBAHeR7Kaz8ruVpOa5R+EJqiRkLyblhD0vzlQiPVVLE8NQsZwjK1YH72hd3m4GtoeS1GfwzyHkLqp+za3xBTbyJbRawQZ3QS+EY5uQKyYUM1wVj6hQcBNlgHLT4rcOmocub+0Vj2Ux7hN+xxLnHeSt5D5zCD2uihoEPGIczJt88GB0hi+JqZitGWs+X0voQouPusAfW4F/qN+mgcnxGrnQ/5raWTFOlLPw/uxNdsZTdQ4fjfGTpOkK859uDN9rjxcvacWMRS4QFqg7UZuS1DQ0cKm6jG4PPBZqPf+4fyLQjZYyssbdQ5lxrO9bO8Bw1Pqza+nfUkN5S5Xf2B5MM4D1Y4ECgYEA/2QpVl31YFaKqDVuF6pdExGK8QIuqbOZmSBa5g0+YY9v0QEGJl61HzHfooHVGlrXa/V6+dL1MLFDD6SvWyVjfSIXSJ4shNQ2L7yBdmd/ZTTYMLNEfaIedJCvAgxI2UfwstsDO3VDz90ZSK7h72JGLjjnIhUznu6l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TkvCLoS8cV8gGJ57BiQqa3Rqjro6/jvc7ByV1fOSxyXIMyuWQIDAQAB`,
  // 必须设置沙箱环境
  gateway: `https://openapi.alipaydev.com/gateway.do`
});

module.exports = app => {
  app.use(bodyParser.json())
  // 列表接口  轮播接口 导航接口
  app.get('/api/lists', (req, res) => {
    const {
      pageSize = 10, pageCode = 1, value = ""
    } = req.query;
    let newLists = [...lists];
    if(value) {
      newLists = newLists.filter(v => JSON.stringify(v).includes(value))
    }

    if (pageSize < 0 || pageCode < 0) {
      res.send({
        code: 400,
        message: '请求参数不合法'
      });
      return;
    }
   //      1
    if (pageCode * pageSize <= newLists.length + pageSize) {
      res.send({
        code: 200,
        // 分页器公式
        data: newLists.slice((pageCode - 1) * pageSize, pageCode * pageSize)
      })
    } else {
      res.send({
        code: 201,
        message: '没有更多数据了'
      })
    }


  })
  app.get('/api/menus', (req, res) => {
    res.send({
      code: 200,
      data: menus
    })
  })
  app.get('/api/banners', (req, res) => {
    res.send({
      code: 200,
      data: banners
    })
  })
  app.get('/api/tabs', (req, res) => {
    res.send({
      code: 200,
      data: tabs
    })
  })

  // 下单支付接口
  app.post('/api/payment', async (req, res) => {
    // 1. 获取支付必要参数
    const { price, id, title } = req.body;
    // 2. 调用支付宝下单接口
    const url = await alipaySdk.pageExec('alipay.trade.wap.pay', {
      // 方式
      method: 'GET',
      // 回调地址
      returnUrl: 'https://www.baidu.com/',
      // 请求参数
      bizContent: {
        out_trade_no: id,
        total_amount: price,
        subject: title
      }
    })
    res.send({
      code: 200,
      data: url
    })
  })  

  app.get('/api/list/:id', (req, res) => {
    // 1. 获取动态路由ID
    const { id } = req.params
    // 2. 根据 id 查找 数据
    const item = lists.find(v => v.id === id);
    // 3. 返回
    res.send({
      code: 200,
      data: item,
      message: 'ok'
    })
  })
}